package proxyElementsExample
{
	import flash.display.Stage;
	
	import org.osmf.elements.DurationElement;
	import org.osmf.elements.ImageElement;
	import org.osmf.elements.SerialElement;
	import org.osmf.elements.VideoElement;
	import org.osmf.events.MediaElementEvent;
	import org.osmf.events.TimelineMetadataEvent;
	import org.osmf.layout.LayoutMetadata;
	import org.osmf.logging.Logger;
	import org.osmf.media.MediaElement;
	import org.osmf.media.MediaPlayerSprite;
	import org.osmf.media.URLResource;
	import org.osmf.metadata.CuePoint;
	import org.osmf.metadata.CuePointType;
	import org.osmf.metadata.TimelineMetadata;

	public class ProxyElementExample
	{
		private var root:Stage;
		private var mediaPlayerSprite:MediaPlayerSprite;
		private var sequence:SerialElement;
		
		public function ProxyElementExample(root:Stage)
		{
			this.root = root;
			mediaPlayerSprite = new MediaPlayerSprite();
			root.addChild(mediaPlayerSprite);
			
			sequence = new SerialElement();
			
			var videoElement1:VideoElement = new VideoElement(new URLResource("http://localhost/vod/sample1_1000kbps.f4v"));
			var durationElement:DurationElement = new DurationElement(5, new ImageElement(new URLResource("ad.png")))
			var videoElement2:VideoElement = new VideoElement(new URLResource("http://localhost/vod/sample1_700kbps.f4v"));
			
			sequence.addChild(videoElement1);
			sequence.addChild(durationElement);//插入广告
			sequence.addChild(videoElement2);
			
			mediaPlayerSprite.media = sequence;
			
			mediaPlayerSprite.media.addEventListener(MediaElementEvent.METADATA_ADD, onMetadataAdd);
			var dynamicTimelineMetadata:TimelineMetadata = new TimelineMetadata(videoElement1);
			videoElement1.addMetadata(CuePoint.DYNAMIC_CUEPOINTS_NAMESPACE, dynamicTimelineMetadata);
			// You can add as many cue points as you like here
			var cuePoint:CuePoint = new CuePoint
				(
					CuePointType.ACTIONSCRIPT, 
					4,//this is a time value in seconds
					"my cue point", 
					{aaaa:"fdafdasfdsf"}//this could be custom name-value pair data
				);
			dynamicTimelineMetadata.addMarker(cuePoint);
			
			if(0)
			{
				var layoutMetedata:LayoutMetadata = new LayoutMetadata();
				layoutMetedata.width = 600;
				layoutMetedata.height = 600;
				mediaPlayerSprite.media.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, layoutMetedata);
			}
		}
		
		private function onMetadataAdd(evt:MediaElementEvent):void
		{
			if (evt.namespaceURL == CuePoint.DYNAMIC_CUEPOINTS_NAMESPACE)
			{
				var timelineMetadata:TimelineMetadata = sequence.getMetadata(CuePoint.DYNAMIC_CUEPOINTS_NAMESPACE) as TimelineMetadata;
				timelineMetadata.addEventListener(TimelineMetadataEvent.MARKER_TIME_REACHED, onCuePoint);
			}
		}
		private function onCuePoint(event:TimelineMetadataEvent):void
		{
			var cuePoint:CuePoint = event.marker as CuePoint;
			trace("Cue Point at ==============================================================" + cuePoint.time);
		}
	}
}